package com.example.shuqishixi;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

@WebServlet(name = "deleteServlet", value = "/deleteServlet")
public class DeleteServlet extends HttpServlet {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "123456";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();

        String videoName = request.getParameter("videoName");
        String username = (String) request.getSession().getAttribute("username");

        if (username == null) {
            out.write("未登录用户不能删除记录");
            return;
        }

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement ps = conn.prepareStatement("DELETE FROM video_analysis WHERE username = ? AND video_name = ?")) {

            ps.setString(1, username);
            ps.setString(2, videoName);
            int rowsAffected = ps.executeUpdate();

            if (rowsAffected > 0) {
                out.write("删除成功");
            } else {
                out.write("未找到要删除的记录");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            out.write("删除失败: " + e.getMessage());
        }
    }
}
